package com.yjs.app.trade.web.controller.sys;

import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yjs.app.trade.core.util.JedisUtil;
import com.yjs.app.trade.core.util.StringUtils;
import com.yjs.app.trade.entity.base.SysConstant;
import com.yjs.app.trade.service.service.business.member.MemberService;
import com.yjs.app.trade.service.service.sys.MessagePushService;
import com.yjs.app.trade.service.service.sys.UserService;
import com.yjs.app.trade.web.controller.base.BaseController;
import com.yjs.app.trade.web.controller.base.util.Result;

import net.sf.json.JSONObject;

@Controller
@RequestMapping(value = "/app/sys/user")
public class UserController extends BaseController {

	@Autowired
	UserService userService;

	@Autowired
	MemberService memberService;
	
	@Autowired
	MessagePushService messagePushService;

	/**
	 * 校验用户原密码是否正确
	 * 
	 * @return
	 */
	@RequestMapping(value = "/checkUserPassword", method = RequestMethod.GET)
	@ResponseBody
	public Object checkUserPassword(String oldPwd) {
		return userService.checkUserPassword(oldPwd);
	}

	/**
	 * 匹配用户认证信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateIdentityAuthentication", method = RequestMethod.POST)
	@ResponseBody
	public Object updateIdentityAuthentication(HttpServletRequest request, String name, String idcardno) {
		return userService.updateIdentityAuthentication(getUserId(request), name, idcardno);
	}

	/**
	 * 修改用户登录设备标识
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateLoginMac", method = RequestMethod.POST)
	@ResponseBody
	public Object updateLoginMac(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String mobile = "";
		if (jsonObject.get("mobile") != null) {
			mobile = jsonObject.getString("mobile");
		} else {
			mobile = userService.getUserInfo(getUserId(request)).get("mobile").toString();
		}
		String authCode = "";
		if (JedisUtil.get(mobileCodeRedisHead + mobile) != null) {
			authCode = JSONObject.fromObject(JedisUtil.get(mobileCodeRedisHead + mobile)).getString("code");
		} else {
			return Result.cfail(SysConstant.ErrorCode.CODE_ERROR, "验证码已经过期!");
		}
		if (StringUtils.equals(authCode, jsonObject.get("authCode").toString())) {
			int result = userService.updateLoginMac(getUserId(request), jsonObject.getString("mac"));
			return result == 1 ? Result.csuccess() : Result.cfail(SysConstant.ErrorCode.BUSINESS_ERROR, "修改失败"); 
		}
		return Result.cfail(SysConstant.ErrorCode.CODE_ERROR, "验证码错误！");
	}

	/**
	 * 修改用户密码
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateUserPassword", method = RequestMethod.POST)
	@ResponseBody
	public Object updateUserPassword(String oldPwd, String newPwd) {
		return userService.updateUserPassword(oldPwd, newPwd);
	}

	/**
	 * 获取用户信息
	 * 
	 * @return
	 */
	@RequestMapping(value = "/getUserInfos", method = RequestMethod.GET)
	@ResponseBody
	public Object getUserInfos(HttpServletRequest request) {
		JSONObject jsonObject = getUserInfo(request);
		String userId = JSONObject.fromObject(jsonObject.get("allAttribute")).getString("PASSPORT_ID");
		String memberName = JSONObject.fromObject(jsonObject.get("allAttribute")).getString("MEMBER_NAME");
		Map<String, Object> user = userService.getUserInfo(userId);
		user.put("memberName", memberName);
		String regionId = "";
		if (user.get("countyRegionId") != null) {
			regionId = user.get("countyRegionId").toString();
		} else if (user.get("cityRegionId") != null) {
			regionId = user.get("cityRegionId").toString();
		} else if (user.get("provinceRegionId") != null) {
			regionId = user.get("provinceRegionId").toString();
		} else {
			return Result.csuccess(user);
		}
		Map<String, Object> regionMap = memberService.getRegionById(regionId);
		if (regionMap != null) {
			user.put("fullName", regionMap.get("fullName"));
			user.put("fullId", regionMap.get("innerCode"));
		}
		return Result.csuccess(user);
	}

	/**
	 * 修改用户行政区域
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateUserRegion", method = RequestMethod.POST)
	@ResponseBody
	public Object updateUserRegion(HttpServletRequest request, String data) {
		JSONObject jsonObject = dpackData(data);
		return Result.csuccess(userService.updateUserRegion(getUserId(request), jsonObject.getString("provinceRegionId"),
						jsonObject.get("cityRegionId") != null ? jsonObject.getString("cityRegionId") : null,
						jsonObject.get("countyRegionId") != null ? jsonObject.getString("countyRegionId") : null,
						jsonObject.getString("address")));
	}

	/**
	 * 修改用户邮箱
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateUserEmail", method = RequestMethod.POST)
	@ResponseBody
	public Object updateUserEmail(String data,HttpServletRequest request) {
		JSONObject dpackData = dpackData(data);
		String passportId = getUserId(request);
		String email = (String) dpackData.get("email");
		int result = userService.updateUserEmail(passportId, email);
		if (result > 0) {
			return Result.csuccess();
		} else {
			return Result.cfail(SysConstant.ErrorCode.BUSINESS_ERROR, "修改失败!");
		}
	}

	/**
	 * 修改用户手机
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateUserPhone", method = RequestMethod.POST)
	@ResponseBody
	public Object updateUserPhone(String data,HttpServletRequest request) {
		JSONObject dpackData = dpackData(data);
		String passportId = getUserId(request);
		String phoneNumber = (String) dpackData.get("phoneNumber");
		int result = userService.updateUserPhone(passportId, phoneNumber);
		if (result > 0) {
			return Result.csuccess();
		} else {
			return Result.cfail(SysConstant.ErrorCode.BUSINESS_ERROR, "修改失败!");
		}
	}

	/**
	 * 绑定手机号码
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateUserMobile", method = RequestMethod.POST)
	@ResponseBody
	public Object updateUserMobile(String data,HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String mobile = "";
		if (jsonObject.get("mobile") != null) {
			mobile = jsonObject.getString("mobile");
		} else {
			mobile = userService.getUserInfo(getUserId(request)).get("mobile").toString();
		}
		String authCode = "";
		if (JedisUtil.get(mobileCodeRedisHead + mobile) != null) {
			authCode = JSONObject.fromObject(JedisUtil.get(mobileCodeRedisHead + mobile)).getString("code");
		} else {
			return Result.cfail(SysConstant.ErrorCode.CODE_ERROR, "验证码已经过期!");
		}
		if (StringUtils.equals(authCode, jsonObject.get("authCode").toString())) {
			String passportId = getUserId(request);
			int result = userService.updateUserMobile(passportId, mobile);
			if (result > 0) {
				return Result.csuccess();
			} else {
				return Result.cfail(SysConstant.ErrorCode.BUSINESS_ERROR, "修改失败!");
			}
		}
		return Result.cfail(SysConstant.ErrorCode.CODE_ERROR,"验证码不正确!!");

	}

	/**
	 * 修改用户头像
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateUserPhoto", method = RequestMethod.POST)
	@ResponseBody
	public Object updateUserPhoto(HttpServletRequest request, String data) {
		JSONObject jsonObject = dpackData(data);
		String photo = jsonObject.getString("photo");
		int result = userService.updateUserPhoto(getUserId(request), photo);
		return result == 1 ? Result.csuccess() : Result.cfail(SysConstant.ErrorCode.BUSINESS_ERROR, "修改失败");
	}
	
	/**
	 * 修改用户推送信息设置
	 * 
	 * @return
	 */
	@RequestMapping(value = "/updateUserJpushStatus", method = RequestMethod.POST)
	@ResponseBody
	public Object updateUserJpushStatus(HttpServletRequest request, String data) {
		JSONObject jsonObject = dpackData(data);
		Map<String, Object> map = new HashMap<>();
		if (StringUtils.strIsNotNull(jsonObject.get("isNewOrder"))) {
			map.put("isNewOrder", jsonObject.getInt("isNewOrder"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("isOrderStatus"))) {
			map.put("isOrderStatus", jsonObject.getInt("isOrderStatus"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("isOrderAog"))) {
			map.put("isOrderAog", jsonObject.getInt("isOrderAog"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("isContractStatus"))) {
			map.put("isContractStatus", jsonObject.getInt("isContractStatus"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("isExpirationContract"))) {
			map.put("isExpirationContract", jsonObject.getInt("isExpirationContract"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("isNewContract"))) {
			map.put("isNewContract", jsonObject.getInt("isNewContract"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("isBargain"))) {
			map.put("isBargain", jsonObject.getInt("isBargain"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("isGmp"))) {
			map.put("isGmp", jsonObject.getInt("isGmp"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("isImportAgencyAgreement"))) {
			map.put("isImportAgencyAgreement", jsonObject.getInt("isImportAgencyAgreement"));
		} else if (StringUtils.strIsNotNull(jsonObject.get("isProductAudit"))) {
			map.put("isProductAudit", jsonObject.getInt("isProductAudit"));
		}
		if (map.size() >= 1) {
			map.put("passportId", getUserId(request));
			userService.updateUserInfo(map);
			return Result.csuccess();
		} else {
			return Result.cfail(SysConstant.ErrorCode.BUSINESS_ERROR, "请传入相关参数");
		}
	}

	/**
	 * 用户登录进行身份认证
	 * 
	 * @param data
	 * @return
	 */
	@RequestMapping(value = "/getUserAuthentication", method = RequestMethod.GET)
	@ResponseBody
	public Object getUserAuthentication(String data, HttpServletRequest request) {
		JSONObject jsonObject = dpackData(data);
		String mobile = "";
		if (jsonObject.get("mobile") != null) {
			mobile = jsonObject.getString("mobile");
		} else {
			mobile = userService.getUserInfo(getUserId(request)).get("mobile").toString();
		}
		String authCode = "";
		if (JedisUtil.get(mobileCodeRedisHead + mobile) != null) {
			authCode = JSONObject.fromObject(JedisUtil.get(mobileCodeRedisHead + mobile)).getString("code");
		} else {
			return Result.cfail(SysConstant.ErrorCode.CODE_ERROR, "验证码已经过期!");
		}
		if (StringUtils.equals(authCode, jsonObject.get("authCode").toString())) {
			Map<String, Object> map = new HashMap<String, Object>();
			map.put("name", jsonObject.get("name"));
			map.put("idcardno", jsonObject.get("idcardno"));
			map.put("mobile", jsonObject.get("mobile"));
			map.put("passportId", getUserId(request));
			int result = userService.getUserAuthentication(map);
			if (result == 1) {
				map.clear();
				map.put("passportId", getUserId(request));
				map.put("identification", "1");// 添加认证状态
				map.put("mac", jsonObject.getString("mac"));
				userService.updateIdEntification(map);
				return Result.csuccess();
			} else {
				return Result.cfail(SysConstant.ErrorCode.BUSINESS_ERROR, "提供的手机号、姓名或者身份证号与系统储存的不一致！");
			}
		}
		return Result.cfail(SysConstant.ErrorCode.CODE_ERROR, "验证码错误！");
	}
	
	/**
	 * 用户消息总数
	 * @return
	 */
	@RequestMapping(value = "/userMessageNum", method = RequestMethod.GET)
	@ResponseBody
	public Object userMessageNum(HttpServletRequest request) {
		String id = getUserId(request);
		Integer number = messagePushService.countUnReadMessageNumber(id);
		return Result.csuccess(number);
	}
}
